﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using GenaralLibs;
using System.Data.SqlClient;
using System.Xml;
using System.Windows.Forms;
using System.Collections.Generic;

namespace Lists
{
    public class DMTieuThucQuanLy : DoiTuong
    {
        #region TenTieuThuc property
        public string TenTieuThuc
        {
            get
            {
                return (string)_DataRow["TenTieuThuc"];
            }
            set
            {
                _DataRow["TenTieuThuc"] = value;
            }
        }
        #endregion
        #region Kieu property
        public string Kieu
        {
            get
            {
                return (string)_DataRow["Kieu"];
            }
            set
            {
                _DataRow["Kieu"] = value;
            }
        }
        #endregion
        #region MoTa property
        public string MoTa
        {
            get
            {
                return (string)_DataRow["MoTa"];
            }
            set
            {
                _DataRow["MoTa"] = value;
            }
        }
        #endregion

        public DMTieuThucQuanLy(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucTieuThucQuanLy : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucTieuThucQuanLy _Instance;
        public static DanhMucTieuThucQuanLy Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucTieuThucQuanLy();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "TieuThucQuanLy";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new DMTieuThucQuanLy((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public DMTieuThucQuanLy this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new DMTieuThucQuanLy(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public DMTieuThucQuanLy NewDoiTuong()
        {
            try
            {
                return new DMTieuThucQuanLy(Instance.DataTable.NewRow());
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DMTieuThucQuanLy GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new DMTieuThucQuanLy(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        public DataTable GetByNganhNgheID(int nganhNgheID)
        {
            SqlConnection con = new SqlConnection(GenaralLibs.LocalSettings.ConnectString);
            try
            {
                con.Open();

                SqlCommand cmd = con.CreateCommand();
                DataSet ds = new DataSet();
                ds.Clear();
                cmd.CommandText = string.Format("Select * from TieuThucQuanLy where ID in(select TieuThucID from NganhNghe_TieuThuc where NganhNgheID = '{0}')",
                                                    nganhNgheID);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                //int i= ds.Tables[0].Rows.Count;
                //quant do vao xml
                //WriteFileXMLMenu(Application.StartupPath+"//Menu.xml", "Menu", ds);
                //Tra ve bag
                DataTable table = new DataTable();
                table.Columns.Clear();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int k = 0; k < ds.Tables[0].Rows.Count; k++)
                    {
                        DataColumn column = new DataColumn();
                        column.Caption = ds.Tables[0].Rows[k][1].ToString();
                        column.ColumnName = ds.Tables[0].Rows[k][1].ToString();
                        table.Columns.Add(column);
                    }
                    List<object> list = new List<object>();
                    for (int j = 0; j < table.Columns.Count; j++)
                    {
                        list.Add(0);
                    }
                    table.Rows.Add();
                    for (int h = 0; h < list.Count; h++)
                    {
                        table.Rows[0][h] = list[h];
                    }

                }

                return table;
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(GenaralLibs.CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
            finally
            {
                con.Close();
            }
        }
        public static void WriteFileXMLMenu(string path, string rootName, DataSet ds)
        {
            XmlTextWriter writer = new
            XmlTextWriter(path, Encoding.UTF8);
            writer.WriteStartDocument();
            writer.WriteStartElement("Root");

            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    writer.WriteStartElement(rootName);
                    writer.WriteAttributeString("ID", i.ToString());
                    writer.WriteElementString("Fields", ds.Tables[0].Rows[i][1].ToString());
                    writer.WriteEndElement();
                }
            }
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
        }
        public static DataTable ReadFileXML(string url)
        {
            XmlDataDocument xmlDataDoc1 = new XmlDataDocument();
            xmlDataDoc1.DataSet.ReadXml(url);
            DataSet ds2 = new DataSet();
            ds2 = xmlDataDoc1.DataSet;
            return ds2.Tables[0];
        }
        public static DanhMucTieuThucQuanLy Filter(string filterExpression)
        {
            try
            {
                DanhMucTieuThucQuanLy obj = new DanhMucTieuThucQuanLy();
                obj.DataView = new DataView(DanhMucTieuThucQuanLy.Instance.DataTable, filterExpression, "ID", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

      
        public DMTieuThucQuanLy GetByID(int objID)
        {
            try
            {
                return GetByProperty("ID", objID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        public DMTieuThucQuanLy GetByName(string name)
        {
            try
            {
                return GetByProperty("TenTieuThuc", name);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        public static DanhMucTieuThucQuanLy GetAll()
        {
            try
            {
                return Instance;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}